package com.itic.codeGen.codeGenerator.util;

import java.util.HashMap;
import java.util.Map;

import org.mybatis.generator.config.Context;

import com.itic.codeGen.codeGenerator.util.CommonUtils.FILE_TYPE;
import com.itic.codeGen.codeGenerator.workLine.PartGenerator;
import com.itic.codeGen.codeGenerator.workLine.implement.ControllerGenerator;
import com.itic.codeGen.codeGenerator.workLine.implement.DaoGenerator;
import com.itic.codeGen.codeGenerator.workLine.implement.DaoImplGenerator;
import com.itic.codeGen.codeGenerator.workLine.implement.ServiceGenerator;
import com.itic.codeGen.codeGenerator.workLine.implement.XmlGenerator;

/**
 * 
 * @author Administrator Inject different parts into final product - imports
 *         getType+"imports" ------ list - header getType+"header" ------ list -
 *         extends "base"+getType ------ String - implements
 *         getType+"implements" ------ list - body getType+"body" ------ list
 * 
 */
public class ParameterUtil {

    // Geneartor
    private static PartGenerator generator;

    /**
     * Custom param parser
     * 
     * @param context
     * @return
     */
    public static Map<String, Object> getJavaParams(Context context) {
	Map<String, Object> params = new HashMap<String, Object>();
	// -------- general params : domainName, package, project location
	// table domainObjectName -- base name
	String domainName = context.getTableConfigurations().get(0)
	        .getDomainObjectName();
	String xmlDomainName = context.getTableConfigurations().get(0)
	        .getTableName();
	if (CommonUtils.testString(domainName)) {
	    params.put("domain", domainName);
	    params.put("xmlDomain", xmlDomainName);
	}
	// model targetPackage -- package name
	// .....model
	String packageName = context.getJavaModelGeneratorConfiguration()
	        .getTargetPackage();
	if (CommonUtils.testString(packageName)) {
	    params.put("package", packageName);
	}
	// model targetProject -- file path
	String projectName = context.getJavaModelGeneratorConfiguration()
	        .getTargetProject();
	if (CommonUtils.testString(projectName)) {
	    params.put("project", projectName);
	}
	// -------- general params ends.
	// -------- custom params, parse extends,import, annotation
	// ....generated baseDao
	String baseDaoClz = context.getJavaClientGeneratorConfiguration()
	        .getTargetPackage();
	// ...generated baseImpl;
	String baseImplClz = context.getJavaClientGeneratorConfiguration()
	        .getImplementationPackage();
	// custom params
	if (CommonUtils.testString(baseDaoClz)
	        && CommonUtils.testString(domainName) && CommonUtils.testString(xmlDomainName)) {
	    // dao
	    params = parseDao(baseDaoClz, packageName, domainName, params);
	    // impl
	    params = parseDaoImpl(baseImplClz, domainName, packageName, params);
	    // controller
	    params = parseController(packageName, domainName, params);
	    // service
	    params = parseService(packageName, domainName, params);
	    // xml
	    params = parseXml(packageName, domainName, xmlDomainName, params);
	}

	return params;
    }

    private static Map<String, Object> parseDao(String baseDaoClz, String packageName,
	    String domainName, Map<String, Object> params) {
	generator = new DaoGenerator(baseDaoClz, packageName, domainName);
	String prefix = FILE_TYPE.DAO.getType();
	params.put(prefix + "imports", generator.getImports());
	params.put(prefix + "header", generator.getHeaders());
	params.put("base" + prefix, generator.getExtend());
	return params;
    }

    // Mybatis3 弃用
    private static Map<String, Object> parseDaoImpl(String baseImplClz,
	    String domainName, String packageName, Map<String, Object> params) {
	generator = new DaoImplGenerator(baseImplClz, domainName, packageName);
	String prefix = FILE_TYPE.IMPL.getType();
	// imports
	params.put(prefix + "imports", generator.getImports());
	// header
	params.put(prefix + "header", generator.getHeaders());
	// extends
	params.put("base" + prefix, generator.getExtend());
	// implements
	params.put(prefix + "implements", generator.getImplement());
	// body
	return params;
    }

    private static Map<String, Object> parseController(String packageName,
	    String domainName, Map<String, Object> params) {
	generator = new ControllerGenerator(packageName, domainName);
	String prefix = FILE_TYPE.CONTROLLER.getType();
	// imports
	params.put(prefix + "imports", generator.getImports());
	// header
	params.put(prefix + "header", generator.getHeaders());
	// extends
	params.put("base" + prefix, generator.getExtend());
	// implements
	// body
	params.put(prefix + "body", generator.getBody());
	return params;
    }

    private static Map<String, Object> parseService(String packageName,
	    String domainName, Map<String, Object> params) {
	generator = new ServiceGenerator(packageName, domainName);
	String prefix = FILE_TYPE.SERVICE.getType();
	// imports
	params.put(prefix + "imports", generator.getImports());
	// header
	params.put(prefix + "header", generator.getHeaders());
	// extends
	params.put("base" + prefix, generator.getExtend());
	// implements
	// body
	params.put(prefix + "body", generator.getBody());
	return params;
    }

    private static Map<String, Object> parseXml(String packageName,
	    String domainName, String xmlDomain, Map<String, Object> params) {
	generator = new XmlGenerator(packageName, domainName, xmlDomain);
	// imports
	// header
	// extender
	// implements
	// body
	params.put("xmlBaseName", generator.getXMLName());
	params.put("xmlSqlBody",generator.getBody());
	return params;
    }

}
